/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PlayerComponent;

import Dispays.DeviceRecord;
import Dispays.DisplayManager;
import PlayerShell.JMplayerLoader;
import java.awt.Component;
import java.awt.Point;
import javax.swing.JRootPane;

/**
 *
 * @author wcss
 */
public class PlayerFrame extends javax.swing.JFrame implements JPlayerResponse {

    public DeviceRecord videoDevice = null;
    public JMediaPlayer videoLocalComponent = null;
    protected Boolean isDeviceRunning = false;
    public Boolean isDisplayDevice = true;
    protected Thread deviceKeepThread = null;
    protected Boolean titleMouseDown = false;
    protected Point origin = new Point();  //全局的位置变量，用于表示鼠标在窗口上的位置

    /**
     * Creates new form PlayerFrame
     */
    public PlayerFrame() {
        // 去掉窗口的装饰
        this.setUndecorated(true);
        //采用指定的窗口装饰风格 
        this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
        initComponents();
        isDeviceRunning = true;
        JMplayerLoader.playerResponse = this;
//        deviceKeepThread = new Thread(this);
//        deviceKeepThread.setDaemon(true);
//        deviceKeepThread.start();
    }

    /**
     * 绑定视频组件
     */
    public void bindVideoComponent() {
        if (videoLocalComponent != null && videoDevice != null) {
            if (videoDevice.eventListener == null) {
                videoDevice.eventListener = new java.awt.event.ComponentAdapter() {

                    @Override
                    public void componentResized(java.awt.event.ComponentEvent evt) {
                        formComponentResized(evt);
                    }

                    @Override
                    public void componentMoved(java.awt.event.ComponentEvent evt) {
                        formComponentMoved(evt);
                    }
                };
                videoLocalComponent.addComponentListener(videoDevice.eventListener);
                videoDevice.localComponent = videoLocalComponent;
                if (isDisplayDevice) {
                    DisplayManager.moveCurrentDevicePanelTo((int) videoDevice.localComponent.getLocationOnScreen().getX(), (int) videoDevice.localComponent.getLocationOnScreen().getY(), videoDevice.localComponent.getWidth(), videoDevice.localComponent.getHeight());
                } else {
                    DisplayManager.hideCurrentDevice();
                }
            }
            this.setAlwaysOnTop(true);
            JMplayerLoader.playerResponse = videoLocalComponent;
        }
    }

    /**
     * 移除视频组件监听
     */
    public void removeVideoComponentListener()
    {
        this.setAlwaysOnTop(false);
        JMplayerLoader.playerResponse = this;
        if (this.videoDevice != null) 
        {
            this.videoDevice.localComponent.removeComponentListener(this.videoDevice.eventListener);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                formComponentMoved(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
        });
        addWindowStateListener(new java.awt.event.WindowStateListener() {
            public void windowStateChanged(java.awt.event.WindowEvent evt) {
                formWindowStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 751, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 506, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * 检查设备显示位置
     */
    public void checkDeviceLocation() {
        try {
            if (titleMouseDown) {
                //标题鼠标按下
            } else {
                if (videoDevice != null && videoDevice.localComponent != null) {
                    //System.out.println("set video location");
                    DisplayManager.moveCurrentDevicePanelTo((int) videoDevice.localComponent.getLocationOnScreen().getX(), (int) videoDevice.localComponent.getLocationOnScreen().getY(), videoDevice.localComponent.getWidth(), videoDevice.localComponent.getHeight());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void formComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentMoved
        // TODO add your handling code here:
        //System.out.println("video move");
        //checkDeviceLocation();
    }//GEN-LAST:event_formComponentMoved

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        // TODO add your handling code here:
        System.out.println("video resize");
        checkDeviceLocation();
    }//GEN-LAST:event_formComponentResized

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        System.out.println("video close");
        isDeviceRunning = false;
        if (videoLocalComponent != null) {
            videoLocalComponent.stopPlay();
        }
    }//GEN-LAST:event_formWindowClosing

    private void formWindowStateChanged(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowStateChanged
        // TODO add your handling code here:
        if (evt.getNewState() == 1) {
            //hide
            System.out.println("video hide");
            isDisplayDevice = false;
            DisplayManager.hideCurrentDevice();
        } else {
            //show
            System.out.println("video show");
            isDisplayDevice = true;
            DisplayManager.showCurrentDevice();
        }
    }//GEN-LAST:event_formWindowStateChanged

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        // TODO add your handling code here:
        origin.x = evt.getX();  //当鼠标按下的时候获得窗口当前的位置
        origin.y = evt.getY();
        if (videoDevice != null)
        {
           DisplayManager.hideCurrentDevice();
        }
    }//GEN-LAST:event_formMousePressed

    private void formMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseDragged
        // TODO add your handling code here:
        //拖动（mouseDragged 指的不是鼠标在窗口中移动，而是用鼠标拖动）
        Point p = this.getLocation();  //当鼠标拖动时获取窗口当前位置
        //设置窗口的位置
        //窗口当前的位置 + 鼠标当前在窗口的位置 - 鼠标按下的时候在窗口的位置
        this.setLocation(p.x + evt.getX() - origin.x, p.y + evt.getY() - origin.y);
    }//GEN-LAST:event_formMouseDragged

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        // TODO add your handling code here:
        if (videoDevice != null)
        {
           checkDeviceLocation();
        }
    }//GEN-LAST:event_formMouseReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    @Override
    public void responseContent(String content, Boolean isErrorText) {
        System.out.println(content);
    }
}
